library(magrittr)
library(tidyverse)
library(Seurat)
library(readxl)
library(cowplot)
library(colorblindr)
library(viridis)
library(magick, lib.loc = "/home/uhlitzf/miniconda3/lib/R/library")
library(ggpubr)
## load global vars: 
source("_src/global_vars.R")

# meta_tbl
# clrs
# markers_v6
# markers_v6_super
# cell_type_super_lookup

names(clrs$cell_type) <- str_replace_all(names(clrs$cell_type), "\\.", " ")
names(clrs$cell_type) <- str_replace_all(names(clrs$cell_type), "Ovarian", "Ov")

## load data --------------------------------------

## load cohort embeddings
seu_tbl <- read_tsv("/work/shah/uhlitzf/data/SPECTRUM/freeze/v6/outs_pre/cells.tsv") %>% 
  mutate(cell_type = ifelse(cell_type == "Monocyte", "Myeloid.cell", cell_type)) %>% 
  mutate(cell_type = ifelse(cell_type == "Ovarian.cancer.cell", "Ov.cancer.cell", cell_type))

## load consensus data
consOV_tbl <- read_tsv("/work/shah/uhlitzf/data/SPECTRUM/freeze/v6/consensusOV/SPECTRUM_freeze_v6_consensusOV.tsv") %>%
  mutate(consensusOV = ordered(consensusOV, levels = names(clrs$consensusOV))) %>% 
  select(cell_id, consensusOV)

## join data
seu_tbl_full <- seu_tbl %>% 
  left_join(meta_tbl, by = "sample") %>% 
  filter(therapy == "pre-Rx", cell_type != "Other", tumor_supersite != "Unknown") %>% 
  rename(UMAP_1 = umap50_1, UMAP_2 = umap50_2) %>% 
  mutate(cell_type_super = cell_type_super_lookup[cell_type]) %>% 
  mutate(cell_type = ordered(str_replace_all(cell_type, "\\.", " "), 
                             levels = names(clrs$cell_type))) %>% 
  mutate(sort_short_x = ifelse(sort_short == "U" & cell_type_super == "Immune", 
                               "CD45+", ifelse(sort_short == "U" & cell_type_super == "Stromal", 
                                               "CD45-", sort_short))) %>% 
  left_join(consOV_tbl, by = "cell_id")

# seu_tbl_full <- seu_tbl_full %>%
#   sample_n(10000)

1 scRNA compositions

source("_src/comp_plot.R")
# rank_by()

comp_tbl_sample <- seu_tbl_full %>%
  filter(therapy == "pre-Rx", cell_type != "Other") %>%
  group_by(tumor_subsite, tumor_supersite, tumor_megasite, patient_id_short,
           therapy, sort_short_x, consensus_signature, cell_type) %>%
  tally() %>%
  group_by(tumor_subsite, tumor_supersite, tumor_megasite, patient_id_short,
           therapy, sort_short_x, consensus_signature) %>%
  mutate(nrel = n/sum(n)*100) %>% 
  mutate(label_supersite = "Site",
         label_therapy = "Rx",
         label_mutsig = "Signature") %>%
  mutate(tumor_supersite = ordered(tumor_supersite, levels = rev(names(clrs$tumor_supersite)))) %>%
  mutate(sample_id = paste(tumor_subsite, patient_id_short, therapy, sort_short_x)) %>%
  group_by(sample_id) %>% 
  mutate(ntotal = sum(n)) %>% 
  filter(ntotal > 0) %>% 
  ungroup()


comp_tbl_consOV <- seu_tbl_full %>%
  filter(therapy == "pre-Rx", cell_type != "Other") %>%
  group_by(tumor_subsite, tumor_supersite, tumor_megasite, patient_id_short,
           therapy, sort_short_x, consensus_signature, consensusOV) %>%
  tally %>%
  group_by(tumor_subsite, tumor_supersite, tumor_megasite, patient_id_short,
           therapy, sort_short_x, consensus_signature) %>%
  mutate(nrel = n/sum(n)*100,
         log10n = log10(n)) %>% 
  mutate(tumor_supersite = ordered(tumor_supersite, levels = rev(names(clrs$tumor_supersite)))) %>%
  mutate(sample_id = paste(tumor_subsite, patient_id_short, therapy, sort_short_x)) %>%
  ungroup


# 
# g9 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45-"),
#                         consensusOV, "Immunoreactive")
# g10 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45-"),
#                          consensusOV, "Mesenchymal")
# g11 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45-"),
#                          consensusOV, "Differentiated")
# g12 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45-"),
#                          consensusOV, "Proliferative")
# 
# g13 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45+"),
#                          consensusOV, "Immunoreactive")
# g14 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45+"),
#                          consensusOV, "Mesenchymal")
# g15 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45+"),
#                          consensusOV, "Differentiated")
# g16 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45+"),
#                          consensusOV, "Proliferative")

# pdf("_fig/002_cohort/002_comp_full.pdf", width = 3.5, height = 12)
# g1;g2;g3;g4;g5;g6;g7;g8;g9;g10;g11;g12;g13;g14;g15;g16
# dev.off()

1.1 cell type

1.1.1 Site

cell_types_immune <- str_replace_all(names(cell_type_super_lookup[cell_type_super_lookup=="Immune"]), "\\.", " ")
cell_types_stromal <- str_replace_all(names(cell_type_super_lookup[cell_type_super_lookup=="Stromal"]), "\\.", " ") %>% 
  str_replace_all("Ovarian", "Ov")
  
for(i in 1:length(cell_types_immune)){
  
  cat('#### ', cell_types_immune[i],' \n')
  
  plist <- default_comp_grid_list(
    filter(comp_tbl_sample, sort_short_x == "CD45+"), 
    cell_type, cell_types_immune[i], cell_type, mutsig_box = F)
  
  p <- plot_grid(plotlist = plist, ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')
  
}

1.1.1.1 B cell

1.1.1.2 Plasma cell

1.1.1.3 T cell

1.1.1.4 Myeloid cell

1.1.1.5 Dendritic cell

for(i in 1:length(cell_types_stromal)){
  
  cat('#### ', cell_types_stromal[i],' \n')
  
  plist <- default_comp_grid_list(
    filter(comp_tbl_sample, sort_short_x == "CD45-"), 
    cell_type, cell_types_stromal[i], cell_type, mutsig_box = F)
  
  p <- plot_grid(plotlist = plist, ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')

}

1.1.1.6 Endothelial cell

1.1.1.7 Fibroblast

1.1.1.8 Ov cancer cell

1.1.2 Signature

cell_types_immune <- str_replace_all(names(cell_type_super_lookup[cell_type_super_lookup=="Immune"]), "\\.", " ")
cell_types_stromal <- str_replace_all(names(cell_type_super_lookup[cell_type_super_lookup=="Stromal"]), "\\.", " ") %>% 
  str_replace_all("Ovarian", "Ov")
  
for(i in 1:length(cell_types_immune)){
  
  cat('#### ', cell_types_immune[i],' \n')
  
  plist <- default_comp_grid_list(
    filter(comp_tbl_sample, sort_short_x == "CD45+"), 
    cell_type, cell_types_immune[i], cell_type, site_box = F)
  
  p <- plot_grid(plotlist = plist, ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')
  
}

1.1.2.1 B cell

1.1.2.2 Plasma cell

1.1.2.3 T cell

1.1.2.4 Myeloid cell

1.1.2.5 Dendritic cell

for(i in 1:length(cell_types_stromal)){
  
  cat('#### ', cell_types_stromal[i],' \n')
  
  plist <- default_comp_grid_list(
    filter(comp_tbl_sample, sort_short_x == "CD45-"), 
    cell_type, cell_types_stromal[i], cell_type, site_box = F)
  
  p <- plot_grid(plotlist = plist, ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')

}

1.1.2.6 Endothelial cell

1.1.2.7 Fibroblast

1.1.2.8 Ov cancer cell

1.2 TCGA

1.2.1 Site

tcga_subtypes <- as.character(unique(consOV_tbl$consensusOV))

for(i in 1:length(tcga_subtypes)){
  
  cat('#### ', tcga_subtypes[i],' \n')
  
  plist <- default_comp_grid_list(
    filter(comp_tbl_consOV, sort_short_x == "CD45+"), 
    consensusOV, tcga_subtypes[i], consensusOV, mutsig_box = F)
  
  p <- plot_grid(plotlist = plist, ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')
  
}

1.2.1.1 Differentiated

1.2.1.2 Immunoreactive

1.2.1.3 Mesenchymal

1.2.1.4 Proliferative

for(i in 1:length(tcga_subtypes)){
  
  cat('#### ', tcga_subtypes[i],' \n')
  
  plist <- default_comp_grid_list(
    filter(comp_tbl_consOV, sort_short_x == "CD45-"), 
    consensusOV, tcga_subtypes[i], consensusOV, mutsig_box = F)
  
  p <- plot_grid(plotlist = plist, ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')

}

1.2.1.5 Differentiated

1.2.1.6 Immunoreactive

1.2.1.7 Mesenchymal

1.2.1.8 Proliferative

1.2.2 Signature

tcga_subtypes <- as.character(unique(consOV_tbl$consensusOV))

for(i in 1:length(tcga_subtypes)){
  
  cat('#### ', tcga_subtypes[i],' \n')
  
  plist <- default_comp_grid_list(
    filter(comp_tbl_consOV, sort_short_x == "CD45+"), 
    consensusOV, tcga_subtypes[i], consensusOV, site_box = F)
  
  p <- plot_grid(plotlist = plist, ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')
  
}

1.2.2.1 Differentiated

1.2.2.2 Immunoreactive

1.2.2.3 Mesenchymal

1.2.2.4 Proliferative

for(i in 1:length(tcga_subtypes)){
  
  cat('#### ', tcga_subtypes[i],' \n')
  
  plist <- default_comp_grid_list(
    filter(comp_tbl_consOV, sort_short_x == "CD45-"), 
    consensusOV, tcga_subtypes[i], consensusOV, site_box = F)
  
  p <- plot_grid(plotlist = plist, ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')

}

1.2.2.5 Differentiated

1.2.2.6 Immunoreactive

1.2.2.7 Mesenchymal

1.2.2.8 Proliferative

2 mpIF composition

## meta data for mpIF
source("_src/global_vars.R")

names(clrs$cell_type) <- str_replace_all(names(clrs$cell_type), "\\.", " ")
names(clrs$cell_type) <- str_replace_all(names(clrs$cell_type), "Ovarian", "Ov")

# mpif_pixel <- read_tsv("/work/shah/vazquezi/data/transfers/spectrum/results/mpif/v8/integrate/outputs/cohort_merge/patient/SPECTRUM/all/detection.tsv") %>%
#   select(cell_id, compartment = Parent)
#
# mpif_cell_type <- read_tsv("/work/shah/vazquezi/data/transfers/spectrum/results/mpif/v8/integrate/outputs/cohort_merge/patient/SPECTRUM/all/cell_type_manual.tsv") %>%
#   left_join(mpif_pixel, by = "cell_id") %>%
#   mutate(cell_id = str_remove_all(cell_id, "CD68.TOX.PD1.PDL1.CD8.panCK_CK8-18.DAPI_|_component_data - resolution #1")) %>%
#   separate(cell_id, into = c("patient_id", "tumor_subsite", "fov_id", "cell_idx"),
#            sep = "_", remove = F) %>%
#   mutate(fov_id = paste0(patient_id, "_", tumor_subsite, "_", fov_id),
#          slide_id = paste0(patient_id, "_", tumor_subsite))
#
# ## expand for double and triple positive cells (cell type markers)
# mpif_ncell <- mpif_cell_type %>%
#   # sample_n(10000) %>%
#   mutate(CD68_state_log = CD68_state == "CD68+",
#          CD8_state_log = CD8_state == "CD8+",
#          panCK_state_log = panCK_state == "panCK+") %>%
#   group_by(cell_id) %>%
#   mutate(n_cells = sum(CD68_state_log, CD8_state_log, panCK_state_log)) %>%
#   select(cell_id, n_cells) %>%
#   deframe
#
# mpif_cell_type_expanded <- bind_rows(
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 0]),
#          TOX_state == "TOX+" | PD1_state == "PD1+" | PDL1_state == "PDL1+") %>%
#     mutate(cell_id = paste0(cell_id, "_0"),
#            cell_type = c("Other")),
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 0]),
#          TOX_state == "TOX-" & PD1_state == "PD1-" & PDL1_state == "PDL1-") %>%
#     mutate(cell_id = paste0(cell_id, "_0"),
#            cell_type = c("Unknown")),
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 1]),
#          CD8_state == "CD8+") %>%
#     mutate(cell_id = paste0(cell_id, "_1"),
#            cell_type = c("CD8+")),
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 1]),
#          CD68_state == "CD68+") %>%
#     mutate(cell_id = paste0(cell_id, "_2"),
#            cell_type = c("CD68+")),
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 1]),
#          panCK_state == "panCK+") %>%
#     mutate(cell_id = paste0(cell_id, "_3"),
#            cell_type = c("panCK+")),
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 2]),
#          CD8_state == "CD8+") %>%
#     mutate(cell_id = paste0(cell_id, "_1"),
#            cell_type = c("CD8+")),
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 2]),
#          CD68_state == "CD68+") %>%
#     mutate(cell_id = paste0(cell_id, "_2"),
#            cell_type = c("CD68+")),
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 2]),
#          panCK_state == "panCK+") %>%
#     mutate(cell_id = paste0(cell_id, "_3"),
#            cell_type = c("panCK+")),
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 3]),
#          CD8_state == "CD8+") %>%
#     mutate(cell_id = paste0(cell_id, "_1"),
#            cell_type = c("CD8+")),
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 3]),
#          CD68_state == "CD68+") %>%
#     mutate(cell_id = paste0(cell_id, "_2"),
#            cell_type = c("CD68+")),
#   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 3]),
#          panCK_state == "panCK+") %>%
#     mutate(cell_id = paste0(cell_id, "_3"),
#            cell_type = c("panCK+"))
# )
#
# mpif_cell_state_expanded <- mpif_cell_type_expanded %>%
#   mutate(cell_state = case_when(
#     cell_type == "CD8+" ~ paste0(CD8_state, TOX_state, PD1_state),
#     cell_type == "CD68+" ~ paste0(CD68_state, PDL1_state),
#     cell_type == "panCK+" ~ paste0(panCK_state, PDL1_state),
#     cell_type == "Unknown" ~ "Unknown",
#     cell_type == "Other" ~ "Other"
#   ))
#
# write_tsv(mpif_cell_state_expanded, "/work/shah/uhlitzf/data/SPECTRUM/mpIF/cell_type_manual_expanded.tsv")

mpif_cell_state_expanded <- read_tsv("/work/shah/uhlitzf/data/SPECTRUM/mpIF/cell_type_manual_expanded.tsv") %>%
  select(cell_id, slide_id, fov_id, compartment, cell_type, cell_state, contains("state")) %>%
  left_join(select(mpif_meta_tbl, slide_id, patient_id_short, tumor_supersite, tumor_subsite, therapy, sample_id, consensus_signature, tumor_megasite), by = "slide_id") %>%
  na.omit() %>% 
  mutate(sort_short_x = compartment)

## cell state composition fov lvl
mpif_cell_state_n <- mpif_cell_state_expanded %>%
  group_by(patient_id_short, tumor_supersite, tumor_subsite, tumor_megasite, therapy, sample_id, fov_id, consensus_signature, cell_state, cell_type) %>%
  tally() %>%
  group_by(patient_id_short, tumor_supersite, tumor_subsite, tumor_megasite, therapy, sample_id, fov_id, consensus_signature) %>%
  mutate(nrel = n/sum(n)) %>%
  ungroup()

## cell state composition slide lvl
mpif_cell_state_n_slide <- mpif_cell_state_expanded %>%
  group_by(patient_id_short, tumor_supersite, tumor_subsite, tumor_megasite, therapy, sample_id, consensus_signature, cell_state, cell_type) %>%
  tally() %>%
  group_by(patient_id_short, tumor_supersite, tumor_subsite, tumor_megasite, therapy, sample_id, consensus_signature) %>%
  mutate(nrel = n/sum(n)) %>%
  ungroup()

## cell state composition slide lvl compartment
mpif_cell_state_n_slide_compartment <- mpif_cell_state_expanded %>%
  group_by(patient_id_short, tumor_supersite, tumor_subsite, tumor_megasite, therapy, sample_id, consensus_signature, sort_short_x, cell_state, cell_type) %>%
  tally() %>%
  group_by(patient_id_short, tumor_supersite, tumor_subsite, tumor_megasite, therapy, sample_id, consensus_signature, sort_short_x) %>%
  mutate(nrel = n/sum(n)*100) %>%
  ungroup()

2.1 cell type

2.1.1 Site

cell_types_mpif <- c("CD8+", "CD68+", "panCK+", "Other", "Unknown")
  
for(i in 1:length(cell_types_mpif)){
  
  cat('#### ', cell_types_mpif[i],' \n')
  
  plist_t <- default_comp_grid_list(
    filter(mpif_cell_state_n_slide_compartment, sort_short_x == "Tumor"), 
    cell_type, cell_types_mpif[i], cell_state, mutsig_box = F)

  plist_s <- default_comp_grid_list(
    filter(mpif_cell_state_n_slide_compartment, sort_short_x == "Stroma"), 
    cell_type, cell_types_mpif[i], cell_state, mutsig_box = F)
  
  pt <- plot_grid(plotlist = plist_t, ncol = 1, align = "v",
                  rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  ps <- plot_grid(plotlist = plist_s, ncol = 1, align = "v",
                  rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  p <- plot_grid(pt, ps, ncol = 2)
  
  print(p)
  
  cat(' \n \n')
  
}

2.1.1.1 CD8+

2.1.1.2 CD68+

2.1.1.3 panCK+

2.1.1.4 Other

2.1.1.5 Unknown

2.1.2 Signature

cell_types_mpif <- c("CD8+", "CD68+", "panCK+", "Other", "Unknown")
  
for(i in 1:length(cell_types_mpif)){
  
  cat('#### ', cell_types_mpif[i],' \n')
  
  plist_t <- default_comp_grid_list(
    filter(mpif_cell_state_n_slide_compartment, sort_short_x == "Tumor"), 
    cell_type, cell_types_mpif[i], cell_state, site_box = F)

  plist_s <- default_comp_grid_list(
    filter(mpif_cell_state_n_slide_compartment, sort_short_x == "Stroma"), 
    cell_type, cell_types_mpif[i], cell_state, site_box = F)
  
  pt <- plot_grid(plotlist = plist_t, ncol = 1, align = "v",
                  rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  ps <- plot_grid(plotlist = plist_s, ncol = 1, align = "v",
                  rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  p <- plot_grid(pt, ps, ncol = 2)
  
  print(p)
  
  cat(' \n \n')
  
}

2.1.2.1 CD8+

2.1.2.2 CD68+

2.1.2.3 panCK+

2.1.2.4 Other

2.1.2.5 Unknown

2.2 cell state

2.2.1 Site

cell_states_mpif <- names(clrs$cell_state)
  
for(i in 1:length(cell_states_mpif)){
  
  cat('#### ', cell_states_mpif[i],' \n')
  
  plist_t <- default_comp_grid_list(
    filter(mpif_cell_state_n_slide_compartment, sort_short_x == "Tumor"), 
    cell_state, cell_states_mpif[i], cell_state, mutsig_box = F)

  plist_s <- default_comp_grid_list(
    filter(mpif_cell_state_n_slide_compartment, sort_short_x == "Stroma"), 
    cell_state, cell_states_mpif[i], cell_state, mutsig_box = F)
  
  pt <- plot_grid(plotlist = plist_t, ncol = 1, align = "v",
                  rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  ps <- plot_grid(plotlist = plist_s, ncol = 1, align = "v",
                  rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  p <- plot_grid(pt, ps, ncol = 2)
  
  print(p)
  
  cat(' \n \n')
  
}

2.2.1.1 CD8+TOX-PD1-

2.2.1.2 CD8+TOX-PD1+

2.2.1.3 CD8+TOX+PD1-

2.2.1.4 CD8+TOX+PD1+

2.2.1.5 CD68+PDL1-

2.2.1.6 CD68+PDL1+

2.2.1.7 panCK+PDL1-

2.2.1.8 panCK+PDL1+

2.2.1.9 Other

2.2.1.10 Unknown

2.2.2 Signature

cell_states_mpif <- names(clrs$cell_state)

for(i in 1:length(cell_states_mpif)){
  
  cat('#### ', cell_states_mpif[i],' \n')
  
  plist_t <- default_comp_grid_list(
    filter(mpif_cell_state_n_slide_compartment, sort_short_x == "Tumor"), 
    cell_state, cell_states_mpif[i], cell_state, site_box = F)

  plist_s <- default_comp_grid_list(
    filter(mpif_cell_state_n_slide_compartment, sort_short_x == "Stroma"), 
    cell_state, cell_states_mpif[i], cell_state, site_box = F)
  
  pt <- plot_grid(plotlist = plist_t, ncol = 1, align = "v",
                  rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  ps <- plot_grid(plotlist = plist_s, ncol = 1, align = "v",
                  rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  p <- plot_grid(pt, ps, ncol = 2)
  
  print(p)
  
  cat(' \n \n')
  
}

2.2.2.1 CD8+TOX-PD1-

2.2.2.2 CD8+TOX-PD1+

2.2.2.3 CD8+TOX+PD1-

2.2.2.4 CD8+TOX+PD1+

2.2.2.5 CD68+PDL1-

2.2.2.6 CD68+PDL1+

2.2.2.7 panCK+PDL1-

2.2.2.8 panCK+PDL1+

2.2.2.9 Other

2.2.2.10 Unknown

3 correlation

# # seu_cohort <- read_rds("/work/shah/isabl_data_lake/analyses/16/52/1652/cohort_merged.rdata")
# #
# # scrna_markers <- as_tibble(cbind(cell_id = colnames(seu_cohort), FetchData(seu_cohort, c("cell_type", "sample", "CD68", "PDCD1", "CD274", "TOX", "CD8A", "CD8B", "KRT8", "KRT19")))) %>%
# #   mutate(CD68_state = ifelse(CD68 > 0, "CD68+", "CD68-"),
# #          CD8_state = ifelse(CD8A > 0 | CD8B > 0, "CD8+", "CD8-"),
# #          panCK_state = ifelse(KRT8 > 0 | KRT19 > 0, "panCK+", "panCK-"),
# #          TOX_state = ifelse(TOX > 0, "TOX+", "TOX-"),
# #          PD1_state = ifelse(PDCD1 > 0, "PD1+", "PD1-"),
# #          PDL1_state = ifelse(CD274 > 0, "PDL1+", "PDL1-"))
# #
# # write_tsv(scrna_markers, "/work/shah/uhlitzf/data/SPECTRUM/freeze/v5/scrna_mpif_marker_expression.tsv")
# 
# scrna_markers <- read_tsv("/work/shah/uhlitzf/data/SPECTRUM/freeze/v5/scrna_mpif_marker_expression.tsv") %>%
#   filter(!(CD68_state == "CD68+" & CD8_state == "CD8+"),
#          !(CD68_state == "CD68+" & panCK_state == "panCK+"),
#          !(CD8_state == "CD8+" & panCK_state == "panCK+")) %>%
#   mutate(cell_type_sc = ifelse(CD68_state == "CD68+", "CD68+", ifelse(CD8_state == "CD8+", "CD8+", ifelse(panCK_state == "panCK+", "panCK+", "Other"))),
#          cell_state = case_when(
#            cell_type_sc == "CD8+" ~ paste0(CD8_state, TOX_state, PD1_state),
#            cell_type_sc == "CD68+" ~ paste0(CD68_state, PDL1_state),
#            cell_type_sc == "panCK+" ~ paste0(panCK_state, PDL1_state),
#            cell_type_sc == "Unknown" ~ "Unknown",
#            cell_type_sc == "Other" ~ "Other"
#          ))
# 
# markers_pos_frac_scrna_celltype <- seu_tbl_full %>%
#   # select(cell_id, sample) %>%
#   select(cell_id, sample, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   left_join(select(scrna_markers, cell_id, cell_type_sc, cell_state), by = "cell_id") %>%
#   na.omit() %>%
#   group_by(sample, cell_type_sc, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   tally %>%
#   group_by(sample, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   mutate(nrel = n/sum(n)) %>%
#   select(sample, cell_type_sc, n, nrel, everything()) %>%
#   ungroup
# 
# markers_pos_frac_scrna_cellstate <- seu_tbl_full %>%
#   # select(cell_id, sample) %>%
#   select(cell_id, sample, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   left_join(select(scrna_markers, cell_id, cell_type_sc, cell_state), by = "cell_id") %>%
#   na.omit() %>%
#   group_by(sample, cell_state, cell_type_sc, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   tally %>%
#   group_by(sample, cell_type_sc, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   mutate(nrel = n/sum(n)) %>%
#   select(sample, cell_type_sc, cell_state, n, nrel, everything()) %>%
#   ungroup
# 
# markers_pos_frac_scrna_gene <- seu_tbl_full %>%
#   # select(cell_id, sample) %>%
#   select(cell_id, sample, cell_type, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   left_join(select(scrna_markers, -sample, -cell_type), by = "cell_id") %>%
#   na.omit() %>%
#   select(-contains("state")) %>%
#   mutate(cell_type = str_replace_all(cell_type, "\\.", " ")) %>%
#   gather(gene, value, -cell_id, -sample, -cell_type, -tumor_supersite,
#          -consensus_signature, -patient_id_short, -sort_short_x, -cell_type_sc) %>%
#   mutate(gene_state = value > 0) %>%
#   group_by(gene, sample, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   mutate(n = sum(gene_state),
#          nrel = n/length(n)) %>%
#   ungroup %>%
#   distinct(gene, sample, tumor_supersite, consensus_signature, patient_id_short, sort_short_x, n, nrel) %>%
#   select(sample, gene, n, nrel, everything())

3.1 mpIF x scRNA correlation

3.1.1 CD45+ cell type correlation

# markers_pos_scrna_mpif <- mpif_cell_state_n_slide_compartment %>%
#   select(sample = sample_cd45p, cell_type_sc = cell_type,
#          compartment, n_mpif = n, nrel_mpif = nrel) %>%
#   group_by(sample, cell_type_sc, compartment) %>%
#   summarise(n_mpif = sum(n_mpif), nrel_mpif = sum(nrel_mpif)) %>%
#   ungroup() %>%
#   left_join(markers_pos_frac_scrna_celltype, by = c("sample", "cell_type_sc")) %>%
#   filter(sort_short_x == "CD45+",
#          cell_type_sc %in% c("CD8+", "CD68+"),
#          compartment %in% c("Stroma", "Tumor"))
# 
# common_layers <- list(
#   facet_wrap(cell_type_sc~compartment, scales = "free"),
#   stat_smooth(aes(nrel, nrel_mpif), method = "lm", color = "black"),
#   stat_cor(aes(nrel, nrel_mpif), method = "spearman", color = "black"),
#   labs(x = "Fraction in scRNA",
#        y = "Fraction in mpIF")
# )
# 
# p1 <- ggplot(markers_pos_scrna_mpif) +
#   geom_point(aes(nrel, nrel_mpif, color = tumor_supersite)) +
#   scale_color_manual(values = clrs$tumor_supersite) +
#   common_layers
# 
# p2 <- ggplot(markers_pos_scrna_mpif) +
#   geom_point(aes(nrel, nrel_mpif, color = consensus_signature)) +
#   scale_color_manual(values = clrs$consensus_signature) +
#   common_layers
# 
# plot_grid(p1, p2)

3.1.2 CD45+ cell state correlation

# markers_pos_scrna_mpif_state <- mpif_cell_state_n_slide_compartment %>%
#   group_by(sample_cd45p, compartment, cell_type) %>%
#   mutate(nrel_state = n/sum(n)) %>%
#   select(sample = sample_cd45p, cell_type_sc = cell_type,
#          compartment, n_mpif = n, nrel_mpif = nrel_state, cell_state) %>%
#   left_join(markers_pos_frac_scrna_cellstate, by = c("sample", "cell_type_sc", "cell_state")) %>%
#   filter(sort_short_x == "CD45+",
#          cell_state %in% c("CD8+TOX-PD1-", "CD8+TOX+PD1+", "CD68+PDL1-", "CD68+PDL1+"),
#          compartment %in% c("Stroma", "Tumor"))
# 
# common_layers <- list(
#   facet_wrap(cell_state~compartment, scales = "free", ncol = 4),
#   geom_smooth(aes(nrel, nrel_mpif), method = "lm", color = "black"),
#   stat_cor(aes(nrel, nrel_mpif), method = "spearman", color = "black"),
#   labs(x = "Fraction in scRNA",
#        y = "Fraction in mpIF")
# )
# 
# p1 <- ggplot(markers_pos_scrna_mpif_state) +
#   geom_point(aes(nrel, nrel_mpif, color = tumor_supersite)) +
#   scale_color_manual(values = clrs$tumor_supersite) +
#   common_layers
# 
# p2 <- ggplot(markers_pos_scrna_mpif_state) +
#   geom_point(aes(nrel, nrel_mpif, color = consensus_signature)) +
#   scale_color_manual(values = clrs$consensus_signature) +
#   common_layers
# 
# plot_grid(p1, p2, ncol = 1)

3.1.3 CD45- cell type correlation

# markers_pos_scrna_mpif_cd45n <- mpif_cell_state_n_slide_compartment %>%
#   select(sample = sample_cd45p, cell_type_sc = cell_type,
#          compartment, n_mpif = n, nrel_mpif = nrel) %>%
#   mutate(sample = str_replace_all(sample, "CD45P", "CD45N")) %>%
#   group_by(sample, cell_type_sc, compartment) %>%
#   summarise(n_mpif = sum(n_mpif), nrel_mpif = sum(nrel_mpif)) %>%
#   ungroup() %>%
#   left_join(markers_pos_frac_scrna_celltype, by = c("sample", "cell_type_sc")) %>%
#   filter(sort_short_x == "CD45-",
#          cell_type_sc %in% c("panCK+"),
#          compartment %in% c("Stroma", "Tumor"))
# 
# common_layers <- list(
#   facet_wrap(cell_type_sc~compartment, scales = "free"),
#   stat_smooth(aes(nrel, nrel_mpif), method = "lm", color = "black"),
#   stat_cor(aes(nrel, nrel_mpif), method = "pearson", color = "black"),
#   labs(x = "Fraction in scRNA",
#        y = "Fraction in mpIF")
# )
# 
# p1 <- ggplot(markers_pos_scrna_mpif_cd45n) +
#   geom_point(aes(nrel, nrel_mpif, color = tumor_supersite)) +
#   scale_color_manual(values = clrs$tumor_supersite) +
#   common_layers
# 
# p2 <- ggplot(markers_pos_scrna_mpif_cd45n) +
#   geom_point(aes(nrel, nrel_mpif, color = consensus_signature)) +
#   scale_color_manual(values = clrs$consensus_signature) +
#   common_layers
# 
# plot_grid(p1, p2)

3.1.4 CD45- cell state correlation

# markers_pos_scrna_mpif_state_cd45n <- mpif_cell_state_n_slide_compartment %>%
#   group_by(sample_cd45p, compartment, cell_type) %>%
#   mutate(nrel_state = n/sum(n)) %>%
#   select(sample = sample_cd45p, cell_type_sc = cell_type,
#          compartment, n_mpif = n, nrel_mpif = nrel_state, cell_state) %>%
#   mutate(sample = str_replace_all(sample, "CD45P", "CD45N")) %>%
#   left_join(markers_pos_frac_scrna_cellstate, by = c("sample", "cell_type_sc", "cell_state")) %>%
#   filter(sort_short_x == "CD45-",
#          cell_state %in% c("panCK+PDL1-", "panCK+PDL1+"),
#          compartment %in% c("Stroma", "Tumor"))
# 
# common_layers <- list(
#   facet_wrap(cell_state~compartment, scales = "free", ncol = 4),
#   geom_smooth(aes(nrel, nrel_mpif), method = "lm", color = "black"),
#   stat_cor(aes(nrel, nrel_mpif), method = "pearson", color = "black"),
#   labs(x = "Fraction in scRNA",
#        y = "Fraction in mpIF")
# )
# 
# p1 <- ggplot(markers_pos_scrna_mpif_state_cd45n) +
#   geom_point(aes(nrel, nrel_mpif, color = tumor_supersite)) +
#   scale_color_manual(values = clrs$tumor_supersite) +
#   common_layers
# 
# p2 <- ggplot(markers_pos_scrna_mpif_state_cd45n) +
#   geom_point(aes(nrel, nrel_mpif, color = consensus_signature)) +
#   scale_color_manual(values = clrs$consensus_signature) +
#   common_layers
# 
# plot_grid(p1, p2, ncol = 1)